﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Tbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
using iMaker.Library;

namespace HKEA.CMS.Core.DAL.Tbl
{
    public class tblHistoryDAL:ItblHistory
    {
        HKEA_CMSEntities model;
        tblHistory history;

        public tblHistoryDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static tblHistoryDAL GetInstance()
        {
            return new tblHistoryDAL();
        }

        public int InsertENT(tblHistory ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.tblHistories.AddObject(ent);
            return model.SaveChanges();
        }

        public IQueryable GetListByENT(tblHistory ent, PagerCollection param, string strPropertyName, string strOrderBy, string dtStart, string dtEnd)
        {
            var result = from h in model.tblHistories
                         join hc in model.mtblHistoryCats on h.HistoryCatUID equals hc.HistoryCatUID
                         where h.HistoryCatUID > 0 && hc.HistoryCatUID > 0
                         select new
                         {
                             h = h,//HistoryCatUID
                             hc = hc//mtblHistoryCats
                         };

            if (ent.HistoryUID > 0)
                result = result.Where(a => a.h.HistoryUID == ent.HistoryUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.h.LastModifyBy == ent.LastModifyBy);
            if (!string.IsNullOrEmpty(ent.History_Detail))
                result = result.Where(a => a.h.History_Detail.Contains(ent.History_Detail));

            if (!string.IsNullOrEmpty(dtStart) )
            {
                DateTime sdt = new DateTime();                
                if (DateTime.TryParse(dtStart, out sdt))
                {
                    sdt = DateTime.Parse(dtStart); 
                    result = result.Where(a => a.h.CreationDate >= sdt);
                }
            }
            if (!string.IsNullOrEmpty(dtEnd))
            { 
                DateTime edt = new DateTime();
                if (DateTime.TryParse(dtEnd, out edt))
                {
                    edt = DateTime.Parse(dtEnd);
                    result = result.Where(a => a.h.CreationDate < edt);
                }
            }

            param.RecordSum = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip(param.RecordStart).Take(param.PageSize);
           
            if (param.RecordSum > 0)
            { 
                param.CurRecordCount = result.Count();
                return result;
            }
            else
                return null;
        }

        public List<tblHistory> GetListByENT(tblHistory ent)
        {
            var result = model.tblHistories.Where(a => a.HistoryUID > 0);
            if (ent.HistoryUID > 0)
                result = result.Where(a => a.HistoryUID == ent.HistoryUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }

        public int GetCountByENT(tblHistory ent)
        {
            var result = model.tblHistories.Where(a => a.HistoryUID > 0);
            if (ent.HistoryUID > 0)
                result = result.Where(a => a.HistoryUID == ent.HistoryUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(tblHistory ent)
        {
            history = model.tblHistories.Where(a=>a.HistoryUID==ent.HistoryUID).FirstOrDefault();
            if (history != null)
            {
                history.LastModifyBy = ent.LastModifyBy;
                history.LastModifyDate = DateTime.Now;
                history.ContactPersonUID = ent.ContactPersonUID;
                history.Cust_MemberUID = ent.Cust_MemberUID;
                history.CustomerUID = ent.CustomerUID;
                history.History_Detail = ent.History_Detail;
                history.HistoryCatUID = ent.HistoryCatUID;
                history.HistoryTypeUID = ent.HistoryTypeUID;
                history.Remark = ent.Remark;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(tblHistory ent)
        {
            var temp = model.tblHistories.Where(a => a.HistoryUID > 0);
            if (ent.HistoryUID > 0)
                temp = temp.Where(a=>a.HistoryUID==ent.HistoryUID);
            var list = temp.ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                model.tblHistories.DeleteObject(list[i]);
            }
            return model.SaveChanges();
        }
    }
}
